Linux环境下的Python翻译工具源码 |
您所在的位置:网站首页 › linux 翻译软件 › Linux环境下的Python翻译工具源码 |
Linux环境下的Python翻译工具源码
玩蛇网Python教程源码示例,本文源码是用于Linux环境下的Python翻译工具源码详解。 学习计算机编程语言一定会涉及到英文和学习应用,但通常英语词典在Linux环境下都不如Win环境下的好用。所以为了方便自己使用,作者花了些时间用python写了个翻译工具。 程序员的编程工作大都在Linux环境下和vim编辑器,基于以上两点出发,开始写代码喽!
使用Python语言编写完成的翻译工具,需要用到urllib和BeautifulSoup 类库。 urllib:主要负责网络通讯方面; BeautifulSoup:负责HTML的解析。 这也是Python语言生态圈的强大之处,有了这两个得力的"助手"写个这样的python翻译工具,毫不费力! 简单的来说下用于Linux环境下的Python翻译工具的原理:获取根据用户输入的单词——提交给百度词典——读取百度词典的翻译——返回的数据并解析——最后将处理过的数据显示出来。如果有能力也可以把Python代码适当的改写一下,把百度翻译换成有道翻译也是可以的。 以下是该Python翻译工具的具体代码 import urllib import codecs from BeautifulSoup import BeautifulSoup from sys import argv import re,time class Translate: def Start(self): self._get_html_sourse() self._get_content("enc") self._remove_tag() self.print_result() def _get_html_sourse(self): word=argv[1] if len(argv)>1 else '' url="http://dict.baidu.com/s?wd=%s&tn=dict" % word self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore") def _get_content(self,div_id): soup=BeautifulSoup("".join(self.htmlsourse)) self.data=str(soup.find("div",{"id":div_id})) def _remove_tag(self): soup=BeautifulSoup(self.data) self.outtext=''.join([element for element in soup.recursiveChildGenerator() if isinstance(element,unicode)]) def print_result(self): for item in range(1,10): self.outtext=self.outtext.replace(str(item),"\n%s" % str(item)) self.outtext=self.outtext.replace(" ","\n") print self.outtext if __name__=="__main__": Translate().Start()Linux环境下的Python翻译工具源码,第二部分: import urllib import codecs from BeautifulSoup import BeautifulSoup from sys import argv import re,time class Translate: def Start(self): self._get_html_sourse() self._get_content("enc") self._remove_tag() self.print_result() def _get_html_sourse(self): word=argv[1] if len(argv)>1 else '' url="http://dict.baidu.com/s?wd=%s&tn=dict" % word self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore") def _get_content(self,div_id): soup=BeautifulSoup("".join(self.htmlsourse)) self.data=str(soup.find("div",{"id":div_id})) def _remove_tag(self): soup=BeautifulSoup(self.data) self.outtext=''.join([element for element in soup.recursi 2000 veChildGenerator() if isinstance(element,unicode)]) def print_result(self): for item in range(1,10): self.outtext=self.outtext.replace(str(item),"\n%s" % str(item)) self.outtext=self.outtext.replace(" ","\n") print self.outtext if __name__=="__main__": Translate().Start()玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/linux/l2552.html 相关文章 Recommend • linux系统 修改include出现问题 • 求大牛看下python源码中的__init__()作用是什么 • 分析python源码找到新闻阅读器无法运行的原因, • Python源码里类似@xxx这样符号是什么意思 • linux + python2 + zsh, unicode编码显示异常 • linux下多个python版本切换如何设置 • Linux控制台终端进行自动处理Web认证方法 • Linux 系统中nginx是否适合用在工控的嵌入式设备上 • 贴个python源码求分析,微信第三方接口无法为客户 • Discuz scrapy模拟登录Python源码如何写 • 文中Python源码中%10的作用是什么 • Python源码中三引号作用是什么 玩蛇网Python互助QQ群,欢迎加入-->: 106381465
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |